#include "line-reader.h"

#include "util/strutil.h"


namespace book
{

std::optional<std::string> LineReader::next() {
    if (!bufferedLines_.empty()) {
        std::optional<std::string> result(std::move(bufferedLines_.front()));
        bufferedLines_.pop_front();
        return result;
    }
    std::string line;
    if (std::getline(in_, line)) {
        strutil::removeLineEnd(line);
        std::optional<std::string> result(std::move(line));
        return result;
    }
    return std::optional<std::string>();
}

void LineReader::putback(std::string &&line) {
    bufferedLines_.push_front(std::move(line));
}


} // namespace book

